<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="承运商统一社会信用代码/身份证" prop="carrierUscc">
        <el-input v-model="formData.carrierUscc" placeholder="请输入承运商统一社会信用代码/身份证" />
      </el-form-item>
      <el-form-item label="承运商/车队长名称" prop="carrierName">
        <el-input v-model="formData.carrierName" placeholder="请输入承运商/车队长名称" />
      </el-form-item>
      <el-form-item label="挂车号" prop="trailerNo">
        <el-input v-model="formData.trailerNo" placeholder="请输入挂车号" />
      </el-form-item>
      <el-form-item label="拖车颜色" prop="trailerPlateColor">
        <el-input v-model="formData.trailerPlateColor" placeholder="请输入拖车颜色" />
      </el-form-item>
      <el-form-item label="道路运输证号" prop="transportNo">
        <el-input v-model="formData.transportNo" placeholder="请输入道路运输证号" />
      </el-form-item>
      <el-form-item label="道路运输证URL" prop="transportUrl">
        <el-input v-model="formData.transportUrl" placeholder="请输入道路运输证URL" />
      </el-form-item>
      <el-form-item label="核发机关" prop="approveOrg">
        <el-input v-model="formData.approveOrg" placeholder="请输入核发机关" />
      </el-form-item>
      <el-form-item label="发证日期" prop="transportIssueDate">
        <el-date-picker
          v-model="formData.transportIssueDate"
          type="date"
          value-format="x"
          placeholder="选择发证日期"
        />
      </el-form-item>
      <el-form-item label="行驶证正面" prop="trailerLicenseFurl">
        <el-input v-model="formData.trailerLicenseFurl" placeholder="请输入行驶证正面" />
      </el-form-item>
      <el-form-item label="行驶证反面" prop="trailerLicenseBurl">
        <el-input v-model="formData.trailerLicenseBurl" placeholder="请输入行驶证反面" />
      </el-form-item>
      <el-form-item label="所有人" prop="owner">
        <el-input v-model="formData.owner" placeholder="请输入所有人" />
      </el-form-item>
      <el-form-item label="档案编号" prop="archivesNo">
        <el-input v-model="formData.archivesNo" placeholder="请输入档案编号" />
      </el-form-item>
      <el-form-item label="使用性质" prop="useNature">
        <el-input v-model="formData.useNature" placeholder="请输入使用性质" />
      </el-form-item>
      <el-form-item label="核定人数" prop="approveNum">
        <el-input v-model="formData.approveNum" placeholder="请输入核定人数" />
      </el-form-item>
      <el-form-item label="拖车识别号" prop="vin">
        <el-input v-model="formData.vin" placeholder="请输入拖车识别号" />
      </el-form-item>
      <el-form-item label="发动机号" prop="engine">
        <el-input v-model="formData.engine" placeholder="请输入发动机号" />
      </el-form-item>
      <el-form-item label="注册日期" prop="registerDate">
        <el-date-picker
          v-model="formData.registerDate"
          type="date"
          value-format="x"
          placeholder="选择注册日期"
        />
      </el-form-item>
      <el-form-item label="发证日期" prop="issueDate">
        <el-date-picker
          v-model="formData.issueDate"
          type="date"
          value-format="x"
          placeholder="选择发证日期"
        />
      </el-form-item>
      <el-form-item label="发证机关" prop="issueOrg">
        <el-input v-model="formData.issueOrg" placeholder="请输入发证机关" />
      </el-form-item>
      <el-form-item label="总重量" prop="totalWeight">
        <el-input v-model="formData.totalWeight" placeholder="请输入总重量" />
      </el-form-item>
      <el-form-item label="整备重量" prop="curbWeight">
        <el-input v-model="formData.curbWeight" placeholder="请输入整备重量" />
      </el-form-item>
      <el-form-item label="准牵引重量" prop="trailerTonnage">
        <el-input v-model="formData.trailerTonnage" placeholder="请输入准牵引重量" />
      </el-form-item>
      <el-form-item label="车长" prop="trailerLen">
        <el-input v-model="formData.trailerLen" placeholder="请输入车长" />
      </el-form-item>
      <el-form-item label="车宽" prop="trailerWidth">
        <el-input v-model="formData.trailerWidth" placeholder="请输入车宽" />
      </el-form-item>
      <el-form-item label="车高" prop="trailerHeight">
        <el-input v-model="formData.trailerHeight" placeholder="请输入车高" />
      </el-form-item>
      <el-form-item label="能源类型" prop="energyType">
        <el-select v-model="formData.energyType" placeholder="请选择能源类型">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="拖车照片" prop="trailerUrl">
        <el-input v-model="formData.trailerUrl" placeholder="请输入拖车照片" />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="formData.remark" placeholder="请输入备注" />
      </el-form-item>
      <el-form-item label="拖车状态" prop="vstatus">
        <el-radio-group v-model="formData.vstatus">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="审核状态" prop="authStatus">
        <el-radio-group v-model="formData.authStatus">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="机构ID" prop="deptId">
        <el-input v-model="formData.deptId" placeholder="请输入机构ID" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { TrailerApi, TrailerVO } from '@/api/base/trailer'

/** 挂车管理 表单 */
defineOptions({ name: 'TrailerForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  carrierUscc: undefined,
  carrierName: undefined,
  trailerNo: undefined,
  trailerPlateColor: undefined,
  transportNo: undefined,
  transportUrl: undefined,
  approveOrg: undefined,
  transportIssueDate: undefined,
  trailerLicenseFurl: undefined,
  trailerLicenseBurl: undefined,
  owner: undefined,
  archivesNo: undefined,
  useNature: undefined,
  approveNum: undefined,
  vin: undefined,
  engine: undefined,
  registerDate: undefined,
  issueDate: undefined,
  issueOrg: undefined,
  totalWeight: undefined,
  curbWeight: undefined,
  trailerTonnage: undefined,
  trailerLen: undefined,
  trailerWidth: undefined,
  trailerHeight: undefined,
  energyType: undefined,
  trailerUrl: undefined,
  remark: undefined,
  vstatus: undefined,
  authStatus: undefined,
  deptId: undefined
})
const formRules = reactive({
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await TrailerApi.getTrailer(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as TrailerVO
    if (formType.value === 'create') {
      await TrailerApi.createTrailer(data)
      message.success(t('common.createSuccess'))
    } else {
      await TrailerApi.updateTrailer(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    carrierUscc: undefined,
    carrierName: undefined,
    trailerNo: undefined,
    trailerPlateColor: undefined,
    transportNo: undefined,
    transportUrl: undefined,
    approveOrg: undefined,
    transportIssueDate: undefined,
    trailerLicenseFurl: undefined,
    trailerLicenseBurl: undefined,
    owner: undefined,
    archivesNo: undefined,
    useNature: undefined,
    approveNum: undefined,
    vin: undefined,
    engine: undefined,
    registerDate: undefined,
    issueDate: undefined,
    issueOrg: undefined,
    totalWeight: undefined,
    curbWeight: undefined,
    trailerTonnage: undefined,
    trailerLen: undefined,
    trailerWidth: undefined,
    trailerHeight: undefined,
    energyType: undefined,
    trailerUrl: undefined,
    remark: undefined,
    vstatus: undefined,
    authStatus: undefined,
    deptId: undefined
  }
  formRef.value?.resetFields()
}
</script>